/**
 * Created by Administrator on 2017/6/27.
 */
define(['swiper', 'jquery', "vue", "popups"], function (swiper, $, Vue, popups) {
    var sw = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        nextButton: '.swiper-button-next',
        prevButton: '.swiper-button-prev',
        paginationClickable: true,
        spaceBetween: 30,
        centeredSlides: true,
        autoplay: 2500,
        autoplayDisableOnInteraction: false
    });
    mui.init();
    var t_price = parseFloat(total_price).toFixed(2);
    var v_price = 0;
    if(document.querySelector('#total_price')!==null){
        document.getElementById("total_price").innerHTML = t_price + "元";
    }

    var vue_popups = new Vue({
        el: '#popups',
        data: {
            v_items: [],
            v_price: 0,
            v_id: 0,
            v_goods_id: 0,
            v_goods_name: "",
            n: null,
            s: null,
            a_index: 0
        },
        methods: {
            variant: function (v_id, v_price, a_index) {
                vue_popups.v_id = v_id;
                vue_popups.v_price = v_price;
                vue_popups.a_index = a_index;//选中的规格
            },
            save_variant: function () {
                var n = vue_popups.n;
                var s = vue_popups.s;
                if (n !== null && s !== null) {
                    var num = n.val();
                    n.val(++num);
                    n.show();
                    s.show();

                    t_price = parseFloat(t_price) + parseFloat(vue_popups.v_price);
                    document.getElementById("total_price").innerHTML = t_price.toFixed(2) + "元";
                    var data = {
                        "type": "add",
                        'variant_id': vue_popups.v_id,
                        "goods_id": vue_popups.v_goods_id
                    };
                    $.post(add_cart_url, data,
                        function (result) {
                            console.log(result);
                            if (result.code !== 1) {
                                mui.alert(result.msg, '系统提示', function () {
                                    location.reload();
                                }, 'div');
                            }else {
                                v_price = vue_popups.v_price;
                            }
                            popups.close(document.querySelector('.popups-module'));
                        });
                }
            }
        }
    });
    var vue = new Vue({
        el: '#data_ul',
        data: {},
        methods: {
            down_tap: function (price, id, v_flag) {
                //event.stopPropagation();
                var target = $(event.currentTarget);
                var n = target.siblings('.num');
                var num = n.val();
                num--;

                if(v_flag===1){
                    if(v_price>0){
                        t_price -= v_price;
                    }else {
                        t_price -= price;
                    }
                }else {
                    t_price -= parseFloat(price);
                }
                n.val(num);
                if (num < 1) {
                    n.hide();
                    target.hide();
                }
                document.getElementById("total_price").innerHTML = Math.abs(t_price).toFixed(2) + "元";
                var data = {
                    "type": "del",
                    'variant_id': 0,
                    "goods_id": id
                };
                $.post(add_cart_url, data,
                    function (result) {
                        if (result.code !== 1) {
                            mui.alert(result.msg, '系统提示', function () {
                                location.reload();
                            }, 'div');
                        }
                    });
            },
            up_tap: function (price, id, v_flag, goods_name) {
                var target = $(event.currentTarget);
                var n = target.siblings('.num');
                var s = target.siblings('.down');
                if (v_flag === 0) {
                    var num = n.val();
                    n.val(++num);
                    n.show();
                    s.show();
                    t_price = parseFloat(t_price) + parseFloat(price);
                    document.getElementById("total_price").innerHTML = t_price.toFixed(2) + "元";
                    var data = {
                        "type": "add",
                        'variant_id': 0,
                        "goods_id": id
                    };
                    $.post(add_cart_url, data,
                        function (result) {
                            if (result.code !== 1) {
                                mui.alert(result.msg, '系统提示', function () {
                                    location.reload();
                                }, 'div');
                            }
                        });
                } else {
                    vue_popups.a_index = 0;
                    vue_popups.v_goods_id = id;
                    vue_popups.v_goods_name = goods_name;
                    vue_popups.n = n;
                    vue_popups.s = s;
                    $.post(variant_url, {"id": id},
                        function (result) {
                            if (result.code === 1) {
                                vue_popups.a_index = 0;
                                vue_popups.v_items = result.data;
                                vue_popups.v_price = vue_popups.v_items[0].price;
                                vue_popups.v_id = vue_popups.v_items[0].id;
                                popups.popups();
                            }
                        });
                }
            },
            send_btn: function (store_id) {
                $.post(cart_url, {store_id: store_id},
                    function (result) {
                        if (result.code === 1) {
                            window.location.href = send_order_url + "/data/" + result.data;
                        } else {
                            mui.alert("您没购买任何商品");
                        }
                    });
            }
        }
    });

    // function getData() {
    //     var data = {
    //         'order': order,
    //         "sort": sort,
    //         'city_searchValue': city_searchValue,
    //         "keyword": keyword,
    //         'page': currentPage
    //     };
    //     $.get(list_url, data,
    //         function (result) {
    //             if (result.code === 1) {
    //                 vue.items.push(result.data);
    //             }
    //         });
    // }

});